﻿<cfsilent>
	<!--- set variable defaults --->
	<cfset variables.CR = 1><!--- default to 1 --->
	<cfset variables.ProgressionRate = 2><!--- default to Medium --->
	<cfset variables.TreasureType = 1><!--- default to Standard --->

	<!--- get values from form --->
	<cfif StructKeyExists(form,"CR")>
		<cfset variables.CR = form.CR>
	</cfif>
	<cfif StructKeyExists(form,"ProgressionRate")>
		<cfset variables.ProgressionRate = form.ProgressionRate>
	</cfif>
	<cfif StructKeyExists(form,"TreasureType")>
		<cfset variables.TreasureType = form.TreasureType>
	</cfif>

	<!--- get lookup values --->
	<cfquery name="variables.GetCRs" datasource="#application.DSN#">
		select min(CR) as minCR, max(CR) as maxCR
		from BaseTreasure
		order by CR
	</cfquery>

	<cfquery name="variables.GetProgressionRates" datasource="#application.DSN#">
		select ProgressionID, ProgressionName
		from ProgressionRates
	</cfquery>

	<cfquery name="variables.GetTreasureTypes" datasource="#application.DSN#">
		select TreasureTypeID, TreasureType
		from TreasureTypes
		order by Multiplier
	</cfquery>
</cfsilent><cfmodule template="../_header.cfm">
	<!--- add data to header here  --->
	<title>Pathfinder Treasure Generator v.0.0.1</title>

	<style type="text/css">
		body {
			font-size: 10pt;
		}

		.buttonWrapper {
			clear: both;
			text-align: center;
			width: 100%;
		}

		#cr {
			border: none;
		}

		#form {
			float: left;
		}
		.fieldWrapper {
			clear: both;
			float: left;
			margin: .3em 0;
		}

		label {
		    float: left;
		    font-weight: bold;
		    margin-right: 0.5em;
		    text-align: right;
		    width: 137px;
		}

		#results {
			border-left: 1px solid black;
			float: left;
			margin-left: 1.5em;
			padding-left: 1.5em;
			width: 450px;
		}

		#results div {
			border: 1px dashed green;
			margin: .5em 0;
		}

		#slider {
			float: right;
			width: 250px;
		}

		#treasureGenerator {
			width: 450px;
		}

		.ui-widget{
			font-size: 1em;
		}
		#results ul {
			list-style-type: none;
		}
		.category {
			font-weight: bold;
		}
		.item {
			padding-left: 3em;
		}
		.itemName {
			font-weight: bold;
		}
	</style>
</cfmodule>
<div id="form">
	<form action="" id="treasureGenerator" method="post">
		<div class="fieldWrapper test"><label for="cr">Challenge Rating:</label><input type="text" name="cr" id="cr" value="<cfoutput>#variables.CR#</cfoutput>" size="3" /><div id="slider"></div></div>
		<div class="fieldWrapper">
			<label for="progressionRate">Progression Rate:</label>
			<select name="progressionRate" id="progressionRate">
				<cfoutput query="variables.GetProgressionRates">
					<option value="#variables.GetProgressionRates.ProgressionID#"<cfif variables.ProgressionRate eq variables.GetProgressionRates.ProgressionID> selected="selected"</cfif>>#variables.GetProgressionRates.ProgressionName#</option>
				</cfoutput>
			</select>
		</div>
		<div class="fieldWrapper">
			<label for="treasureType">Treasure Type:</label>
			<select name="treasureType" id="treasureType">
				<cfoutput query="variables.GetTreasureTypes">
					<option value="#variables.GetTreasureTypes.TreasureTypeID#"<cfif variables.TreasureType eq variables.GetTreasureTypes.TreasureTypeID> selected="selected"</cfif>>#variables.GetTreasureTypes.TreasureType#</option>
				</cfoutput>
			</select>
		</div>
		<div class="buttonWrapper">
			<button id="getTreasure">Get Full Treasure!</button>
			<br/>
			<button id="getBaseTreasure">Get Base Treasure!</button>
		</div>
		<div class="fieldWrapper">
			<label for="itemCategoryID">Item Category:</label>
			<select name="itemCategoryID" id="itemCategoryID">
				<option value="1">Minor</option>
				<option value="2">Medium</option>
				<option value="3">Major</option>
			</select>
		</div>
		<div class="fieldWrapper">
			<label for="isMagic">Magic:</label>
			<select name="isMagic" id="isMagic">
				<option value="0">No</option>
				<option value="1">Yes</option>
			</select>
		</div>
		<div class="fieldWrapper">
			<label for="itemTypeID">Item Type:</label>
			<select name="itemTypeID" id="itemTypeID">
				<option value="8">Ring</option>
				<option value="9">Minor Wondrous</option>
				<option value="11">Staff</option>
				<option value="12">Rod</option>
				<option value="13">Med Wondrous</option>
				<option value="14">Maj Wondrous</option>
			</select>
		</div>
		<div class="buttonWrapper">
			<button id="getWeapon">Get Weapon!</button>
			<br/>
			<button id="getArmor">Get Armor!</button>
			<br/>
			<button id="getShield">Get Shield!</button>
			<br/>
			<button id="getMagicItem">Get Magic Item!</button>
			<br/>
			<button id="getMinorItem">Get Minor Item!</button>
			<br/>
			<button id="getMediumItem">Get Medium Item!</button>
			<br/>
			<button id="getMajorItem">Get Major Item!</button>
			<br/>
			<button id="getPotion">Get Potion!</button>
			<br/>
			<button id="getScroll">Get Scroll!</button>
			<br/>
			<button id="getWand">Get Wand!</button>
			<br/>
			<button id="getGoods">Get Goods!</button>
			<br/>
			<button id="getTradeGoods">Get Trade Goods!</button>
			<br/>
			<button id="getItem">Get Item!</button>
		</div>
	</form>
</div>
<div id="results"></div>
<cfmodule template="../_footer.cfm">
	<script type="text/javascript">
		$(document).ready( function() {
			$('button').not('#getTreasure, #getBaseTreasure').button()
				.click( function(e) {
					e.preventDefault();

					<!--- post form to cfcs --->
					var $form = $('#treasureGenerator');
					var data = {
						method: this.id,
						cr: $form.find($('#cr')).val(),
						progressionID: $form.find($('#progressionRate')).val(),
						treasureTypeID: $form.find($('#treasureType')).val(),
						itemCategory: $form.find($('#itemCategoryID')).val(),
						isMagic: $form.find($('#isMagic')).val(),
						itemTypeID: $form.find($('#itemTypeID')).val()
					}
					$.ajax({
						data: data,
						success: function(data){
//							var data = eval("(" + data + ")");
							var $destination = $('#results').prepend('<div><ul></ul></div>');
							$destination = $destination.children(':first').find('ul');
console.log(data);
//							$destination.append('<li class="category">Name: ' + data.ITEMTYPE + '</li>');
$destination.append(data);
							<!---$.each(data.ITEM, function(index, value){
								if (index != "Name" && index != "Price") {
									$destination.append('<li class="category">' + index + ': ' + value + '</li>');
								}
							});--->
						}
					});
				});

			$('#getBaseTreasure').button()
				.click( function(e) {
					e.preventDefault();

					<!--- post form to cfcs --->
					var $form = $('#treasureGenerator');
					var data = {
						method: this.id,
						cr: $form.find($('#cr')).val(),
						progressionID: $form.find($('#progressionRate')).val(),
						treasureTypeID: $form.find($('#treasureType')).val()
					}
					$.ajax({
						data: data,
						success: function(data){
							var data = eval("(" + data + ")");
							var $destination = $('#results').prepend('<div><ul></ul></div>');
							$destination = $destination.children(':first').find('ul');

							$.each(data.GetBaseTreasureInfo.COLUMNS, function(index, value){
								$destination.append('<li class="category">' + value + ': ' + data.GetBaseTreasureInfo.DATA[0][index] + '</li>');
							});
						}
					});
				});

			$('#getTreasure').button()
				.click( function(e) {
					e.preventDefault();

					<!--- post form to cfcs --->
					var $form = $('#treasureGenerator');
					var data = {
						method: 'getTreasure',
						cr: $form.find($('#cr')).val(),
						progressionID: $form.find($('#progressionRate')).val(),
						treasureTypeID: $form.find($('#treasureType')).val(),
					}
					$.ajax({
						data: data,
						success: function(data) {

							if (data) {
								var data = eval("(" + data + ")");
								console.log(data);
								var $destination = $('#results').prepend('<div><ul></ul></div>');
								$destination = $destination.children(':first').find('ul');
								$destination.append('<li class="category">Total GPV: ' + data.TotalGPV + ' [' + data.MaxGPV + ']</li>');
								$.each(data, function(index, value) {

									if (index != "TotalGPV" && index != "MaxGPV" && value.length != 0) {
										$destination.append('<li class="category">' + index + '</li>');
										console.log(value);
										$.each(value, function(index, data) {
											console.log(data);
											$destination.append('<li class="item"><span class="itemName">' + data.NAME + '</span> <span class="itemData">(' + data.PRICE + ' gp)</span></li>');
											$.each(data, function(index, value) {

									if (index != "NAME" && index != "PRICE") {
										$destination.append('<li class="item"><span class="itemName">' + index + '</span>: <span class="itemData">' + value + '</span></li>');
												}
											});
										});
									}
								});
							}
						}
				});
			});

			$( "#slider" ).slider({
				value: <cfoutput>#variables.CR#</cfoutput>,
				min: <cfoutput>#variables.GetCRs.MinCR#</cfoutput>,
				max: <cfoutput>#variables.GetCRs.MaxCR#</cfoutput>,
				slide: function( event, ui ) {
					$( "#cr" ).val( ui.value );
				}
			});
		});

		$.ajaxSetup({
			data: {
				returnformat: 'json'
			},
			type: 'post',
			url: '/pathfinder/cfcs/treasureGenerator.cfc'
		});
	</script>
</cfmodule>